package org.openharmony.qttool;

import com.intellij.openapi.util.io.FileUtil;

import javax.swing.*;
import javax.swing.event.ListDataListener;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class QtSDKManager {
    private static final QtSDKManager INSTANCE = new QtSDKManager();
    private static final String CONFIG_FILE = "config.data";
    private ArrayList<QtSDK> qtSDKList = new ArrayList<>();
    QtSDKManager() {
        if (FileUtil.exists(config()))
            load();
    }

    public static QtSDKManager getInstance() {
        return INSTANCE;
    }

    public static ArrayList<QtSDK> getQtSDKList() {
        return INSTANCE.qtSDKList;
    }

    public int count() {
        return qtSDKList.size();
    }

    public ArrayList<QtSDK> allKits() {
        return qtSDKList;
    }

    public QtSDK get(int index) {
        if (index < 0 || index >= qtSDKList.size())
            return null;
        return qtSDKList.get(index);
    }

    public void remove(QtSDK sdk) {
        if (contains(sdk))
            qtSDKList.remove(sdk);
    }

    public boolean contains(QtSDK sdk) {
        return qtSDKList.contains(sdk);
    }

    public static boolean hasSDK() {
        return !INSTANCE.qtSDKList.isEmpty();
    }

    public boolean add(String path) {
        QtSDK qt = new QtSDK(path);
        if (!qt.isValid())
            return false;
        qtSDKList.add(qt);
        return true;
    }

    public boolean add(QtSDK sdk) {
        if (!sdk.isValid())
            return false;
        qtSDKList.add(sdk);
        return true;
    }

    public static ComboBoxModel<QtSDK> model() {
        return new QtSDKModel(INSTANCE);
    }

    public void load() {
        try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(config()))) {
            Object obj = objectInputStream.readObject();
            ArrayList<QtSDK> result = new ArrayList<>();
            if (obj instanceof ArrayList<?>) {
                for (Object o : (ArrayList<?>) obj) {
                    result.add((QtSDK) o);
                }
            }
            qtSDKList = result;
            System.out.println("Objects deserialized successfully.");
        } catch (IOException | ClassNotFoundException e) {
            Utils.handleException(e);
        }
    }

    private static String config() {
        Map<String, String> env = System.getenv();
        Path path = Paths.get(env.get("LOCALAPPDATA"), "ohosqttool", CONFIG_FILE);
        return path.toString();
    }

    public static void save() {
        String filePath = config();
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.getParentFile().mkdirs();
                file.createNewFile();
            } catch (IOException e) {
                System.err.println("Error creating file: " + e.getMessage());
            }
        }
        try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(filePath))) {
            outputStream.writeObject(INSTANCE.qtSDKList);
            System.out.println("Objects serialized successfully.");
        } catch (IOException e) {
            Utils.handleException(e);
        }
    }
}
